#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import requests
from typing import List, Dict
from utils.utils import setup_logging
from config import REPO_OWNER, REPO_NAME, ACCESS_TOKEN, GITEE_API_BASE, REQUEST_TIMEOUT

logger = setup_logging("fetch_commits")

def fetch_pr_commits(pr_number: int, access_token: str = ACCESS_TOKEN) -> List[Dict]:
    """获取单个PR的commit信息"""
    url = f"{GITEE_API_BASE}/repos/{REPO_OWNER}/{REPO_NAME}/pulls/{pr_number}/commits"
    headers = {
        "Authorization": f"token {access_token}",
        "Accept": "application/json"
    }
    try:
        response = requests.get(url, headers=headers, timeout=REQUEST_TIMEOUT)
        if response.status_code != 200:
            logger.error(f"PR#{pr_number} commit请求失败，状态码: {response.status_code}")
            return None
        return response.json()
    except Exception as e:
        logger.exception(f"PR#{pr_number} commit请求异常: {e}")
        return None

def extract_commit_messages(commits: List[Dict]) -> List[str]:
    """从commit列表中提取commit messages"""
    commit_messages = []
    if commits:
        for c in commits:
            msg = c.get("commit", {}).get("message")
            if msg:
                commit_messages.append(msg)
    return commit_messages 